using System.Web.Mvc;
using Orange.Mvc4App.Models;
using Orange.Mvc4App.Services;
using System.Linq;
using System.Collections.Generic;
using System;
using Orange.Mvc4App.Infrastructure;

namespace Orange.Mvc4App.Controllers {
    public class GroupController : BaseController {
        private readonly IGroupService service;

        public GroupController(IGroupService groupService) {
            service = groupService;
        }

        public ViewResult Index(string ddlgroup = null) {
            var allGroups = service.GetList(null);
            if (allGroups.Count == 0) {
                return View("None");
            }
            var groupid = ddlgroup ?? allGroups.First().Groupid;
            ViewBag.Groups = allGroups;
            ViewBag.Groupid = groupid;
            ViewBag.Member = service.GetMember(groupid);
            ViewBag.NoMemeber = service.GetNoMember(groupid);
            
            return View();
        }

        public ViewResult Details(string id) {
            return View(service.Find(id));
        }

        public ActionResult Create() {
            return View(new Group());
        }

        [HttpPost]
        public ActionResult Create(Group g) {
            try {
                if (ModelState.IsValid) {
                    service.Add(g);
                    service.Save();
                    return RedirectToAction("Index");
                }
            } catch (Exception ex) {
                ModelState.AddModelError("", ex.Message);
            }
            return View(g);

        }

        public ActionResult Edit(string id) {
            return View(service.Find(id));
        }

        [HttpPost]
        public ActionResult Edit(Group group) {
            if (ModelState.IsValid) {
                service.Update(group);
                service.Save();
                return RedirectToAction("Index");
            } else {
                return View();
            }
        }

        public ActionResult Delete(string id) {
            var model = service.Find(id);
            service.Delete(model);
            service.Save();
            return RedirectToAction("Index");
        }

        [HttpPost]
        public void SaveMem(string memlist, string selgroupid) {
            DateTime dcTime = DateTime.Now;
            var orgMemIds= service.GetMember(selgroupid).Select(c=>c.CustomerId);
            var modifyMemIds = memlist.Split(',').Select(c => int.Parse(c));
            var delMemIds = orgMemIds.Except(modifyMemIds);
            var addMemIds = modifyMemIds.Except(orgMemIds);
            foreach (var id in delMemIds) {
                service.RemoveMemeber(new CustomerGroup {
                    Customerid = id, Groupid = selgroupid, CreateUser = User.Identity.Name, CreateTime = dcTime
                });
            }

            foreach (var id in addMemIds) {
                service.AddMemeber(new CustomerGroup {
                    Customerid = id, Groupid = selgroupid, CreateUser = User.Identity.Name, CreateTime = dcTime
                });
            }

            service.Save();
        }

        protected override void Dispose(bool disposing) {
            if (disposing) {
                service.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

